package cn.zy.jboa.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.zy.jboa.entity.BizLeave;
import cn.zy.jboa.entity.SysEmployee;
import cn.zy.jboa.service.BizLeaveService;
import cn.zy.jboa.util.PageSupport;

import com.opensymphony.xwork2.ActionSupport;

public class BizLeaveAction extends ActionSupport{
	
	@Autowired
	@Qualifier("bizLeaveService")
	BizLeaveService bizLeaveService;
	
	public void setBizLeaveService(BizLeaveService bizLeaveService) {
		this.bizLeaveService = bizLeaveService;
	}

	String startDate=null;
	String endDate=null;
	
	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	PageSupport pageSupport = new PageSupport();
	
	public PageSupport getPageSupport() {
		return pageSupport;
	}

	public void setPageSupport(PageSupport pageSupport) {
		this.pageSupport = pageSupport;
	}

	int pageNo = 1;
	
	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public String searchLeave(){
		SysEmployee employee =(SysEmployee) ServletActionContext.getRequest().getSession().getAttribute("employee");
		System.out.println(employee.getSysDepartment().getName());
		String position = (String) ServletActionContext.getRequest().getSession().getAttribute("employee_position");
		pageSupport.setPageSize(5);
		pageSupport.setCurrentPageNo(this.pageNo);
		pageSupport.setTotalCount(bizLeaveService.getCount(employee.getSn(),position,startDate,endDate));
		pageSupport.setItems((List) bizLeaveService.leaveList(employee.getSn(),
				position,(pageSupport.getCurrentPageNo()-1)*pageSupport.getPageSize(),
				pageSupport.getPageSize(),startDate,endDate));
		ServletActionContext.getRequest().getSession().setAttribute("pageSupport", pageSupport);
		return SUCCESS;
	}
	BizLeave leave ;
	
	public BizLeave getLeave() {
		return leave;
	}

	public void setLeave(BizLeave leave) {
		this.leave = leave;
	}

	public String getLeaveById(){
		leave=bizLeaveService.getLeaveId(leave.getId());
		return SUCCESS;
	}
	
	public String updateCheckLeave(){
		BizLeave leaves = bizLeaveService.getLeaveId(leave.getId());
		Timestamp date = new Timestamp(new Date().getTime());
		leaves.setModifytime(date);
		leaves.setApproveOpinion(leave.getApproveOpinion());
		leaves.setStatus(leave.getStatus());
		bizLeaveService.updateLeave(leaves);
		return SUCCESS;
	}
	public void a (){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		BizLeaveService a = (BizLeaveService) ctx.getBean("bizLeaveService");
		BizLeave leaves = a.getLeaveId(leave.getId());
		Timestamp date = new Timestamp(new Date().getTime());
		leaves.setModifytime(date);
		leaves.setApproveOpinion(leave.getApproveOpinion());
		leaves.setStatus(leave.getStatus());
		a.updateLeave(leaves);
	}
	
	Map leaveTypeMap =new HashMap();
	
	public Map getLeaveTypeMap() {
		leaveTypeMap.put(0, "请选择");
		leaveTypeMap.put(1, "事假");
		leaveTypeMap.put(2,"年假");
		leaveTypeMap.put(3,"病假");
		leaveTypeMap.put(4,"婚假");
		return leaveTypeMap;
	}

	public void setLeaveTypeMap(Map leaveTypeMap) {
		this.leaveTypeMap = leaveTypeMap;
	}

	public String addLeave(){
		return SUCCESS;
	}
	public String saveLeave(){
		leave.setApproveOpinion(null);
		leave.setStatus("待审批");
		Timestamp date = new Timestamp(new Date().getTime());
		leave.setCreatetime(date);
		leave.setModifytime(null);
		if(bizLeaveService.saveLeave(leave)>0){
			System.out.println("添加成功");
		}
		return SUCCESS;
	}
}
